package org.jaaksi.demo.ptr.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import in.srain.cube.views.ptr.header.StoreHouseHeader;
import java.io.IOException;
import org.jaaksi.demo.ptr.R;
import org.jaaksi.demo.ptr.adapter.BaseListAdapter;
import org.jaaksi.demo.ptr.adapter.HouseAdapter;
import org.jaaksi.demo.ptr.api.NetApi;
import org.jaaksi.demo.ptr.bean.house.HouseListBean;
import org.jaaksi.demo.ptr.bean.house.MainResponse;
import org.jaaksi.demo.ptr.bean.response.Result;
import org.jaaksi.demo.ptr.ui.base.BaseListActivity;
import org.jaaksi.demo.ptr.util.DensityUtil;
import retrofit.call.SimpleCallback;
import retrofit.util.ServiceGenerator;
import retrofit2.Call;

/**
 * Created by Fcy on 2016/3/21.<br/>
 */
public class LoadMoreActivity extends BaseListActivity<HouseListBean> {
  final String[] mStringList = { "Alibaba", "TMALL 11-11" };

  public static void goTo(Context context) {
    Intent intent = new Intent(context, LoadMoreActivity.class);
    context.startActivity(intent);
  }

  @Override protected void setContentView() {
    setContentView(R.layout.activity_loadmore);
  }

  @Override protected View initPtrHeader() {
    // header
    final StoreHouseHeader header = new StoreHouseHeader(this);
    header.setPadding(0, DensityUtil.dip2px(this, 15), 0, 0);
    header.setTextColor(Color.parseColor("#888888"));
    /**
     * using a string, support: A-Z 0-9 - .
     * you can add more letters by {@link in.srain.cube.views.ptr.header.StoreHousePath#addChar}
     */
    // 还可以切换
    header.initWithString(mStringList[0], 18);
    return header;
  }

  @Override protected BaseListAdapter<HouseListBean> getAdapter() {
    return new HouseAdapter(this);
  }

  @Override protected void buildRequest() {

    Call<Result<MainResponse>> call = ServiceGenerator.createService(NetApi.class)
      .getMainResponse("110000", getLoadOffset(), getPageSize());
    // 如果是统一请求则无需重新创建，只需要call.clone
    String url = call.request().url().toString();
    Log.i("fcy", "----------------- url=" + url);
    //call.clone().enqueue();
    //Call<Result<MainResponse>> clone = call.clone();

    call.enqueue(new SimpleCallback<Result<MainResponse>>() {
      @Override public void networkError(Call<Result<MainResponse>> call, IOException ex) {
        Toast.makeText(LoadMoreActivity.this, "网络异常", 1).show();
        //setDatas(null, true);
        netError();
      }

      @Override public void onResponse(Call<Result<MainResponse>> call, Result<MainResponse> entity,
        boolean isSuccess) {
        if (isSuccess && entity.data != null && entity.data.resblock_list != null) {
          // 如果使用服务器hasMore字段，则调用setDatas两个参数的方法，
          // 否则要调用initTotalPages设置总个数，且调用一个参数的方法
          // initTotalPages(
          // response.data.resblock_list.total_count);
          setDatas(entity.data.resblock_list.list, entity.data.resblock_list.has_more_data == 1);
        } else { // 如果list=null，就设置为null,true
          setDatas(null, true);
        }
      }
    });
  }
}
